﻿using B.S.DataBase.MyProject.ErrorCode;
using B.S.Traceability.Domain;
using B.S.Traceability.Infrastructure.Interface;
using B.S.Traceability.Read.API.Applicantion.Command.EnterInfoCommand;
using MediatR;

namespace B.S.Traceability.Read.API.Applicantion.CommandHandler.EnterInfoCommandHandler
{
    public class QueryEnterInfoCommandHandler : IRequestHandler<QueryEnterInfoCommand, APIResult<List<SU_EnterInfo>>>
    {
        private readonly IBaseRepository<SU_EnterInfo> enterRep;
        public QueryEnterInfoCommandHandler(IBaseRepository<SU_EnterInfo> enterRep)
        {
            this.enterRep = enterRep;
        }
        public Task<APIResult<List<SU_EnterInfo>>> Handle(QueryEnterInfoCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<SU_EnterInfo>> result = new APIResult<List<SU_EnterInfo>>();
            var firstItem = enterRep.GetAll().Result.Where(x => x.ProductionCode == request.ProductionCode);

            if (firstItem != null)
            {
                result.Code = APIEnum.查询成功;
                result.Msg = APIEnum.查询成功.ToString();
                result.data = firstItem.ToList();
            }
            else
            {
                result.Code = APIEnum.查询失败;
                result.Msg = APIEnum.查询失败.ToString();
                result.data = firstItem.ToList();
            }
            return Task.FromResult(result);
        }
    }
}
